﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;
using Model;
using System.Data.SqlClient;
using Components;

namespace MsDAL
{
    public class PopDal
    {
        public DataTable GetPopData(string tableName, string fields, string otherFieldName, string order, string where, string otherWhere, int rownum)
        {
            string sql = string.Format("SELECT distinct TOP {6} {0} FROM ( SELECT {1} FROM {2} WHERE {3} ) T WHERE {4} {5}",
                otherFieldName, fields, tableName, (string.IsNullOrEmpty(where) ? " 1 = 1 " : where),
                (string.IsNullOrEmpty(otherWhere) ? " 1 = 1 " : otherWhere),
                (string.IsNullOrEmpty(order) ? "" : " order by " + order), rownum == -1 ? 1000 : rownum);

            //Logger.Trace(sql);
            return DBHelper.GetData(sql);
        }

        public DataTable GetPopItem(string key)
        {
            string sql = "SELECT [popno],[ptable],[pkey],[fields],[pagesize],"
            + "[orderfid],[strwhere],[keyref],[reffield],[issavekey],[description],[width],[height]"
            + " FROM Sys8a00 WHERE popno = @popno";

            return DBHelper.GetData(sql, new SqlParameter("@popno", key));
        }
    }
}
